﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace text1
{
    public partial class fzxx : Form
    {
        //数据库连接字符串
        string constr = "server=LAPTOP-NHBSREVG;database=社区数据库;uid=sa;pwd=990823";
        public fzxx()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("男");
            comboBox1.Items.Add("女");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获得文本框的值
            String text1 = textBox1.Text.Trim();
            String text2 = textBox2.Text.Trim();
            String text3 = comboBox1.Text.Trim();
            String text4 = textBox4.Text.Trim();
            String text5 = textBox5.Text.Trim();


            //参数
            SqlParameter para_text1;
            SqlParameter para_text2;
            SqlParameter para_text3;
            SqlParameter para_text4;
            SqlParameter para_text5;
 


            //创建连接并打开
            SqlConnection cnn = new SqlConnection(constr);
            cnn.Open();

            //创建查询语句
            String find = "select * from 房主 ";


            //根据textbox的值是否为空，进行进一步的判断，将sql语句补充完整
            if (text1.Length != 0 || text2.Length != 0 || text3.Length != 0 || text4.Length != 0 || text5.Length != 0)
            {
                find += "where ";
            }
            if (text1.Length != 0)
            {
     
                find += " 房主身份证号=@text1 ";
            }
            if (text2.Length != 0)
            {
                if (text1.Length != 0)
                    find += " and ";
                    find += " 姓名=@text2 ";
            }
            if (text3.Length != 0)
            {
                if (text1.Length != 0 || text2.Length!=0)
                    find += " and ";
                find += " 性别=@text3 ";
            }
            if (text4.Length != 0)
            {
  

                find += "and 生日=@text4 ";
            }
            if (text5.Length != 0)
            {

                find += "and 电话号码=@text5 ";
            }


            //创建sqlDataAdapter
            SqlDataAdapter myDa = new SqlDataAdapter(find, cnn);
            if (text1.Length != 0)
            {
                para_text1 = new SqlParameter("@text1", text1);
                myDa.SelectCommand.Parameters.Add(para_text1);
            }
            if (text2.Length != 0)
            {
                para_text2 = new SqlParameter("@text2", text2);
                myDa.SelectCommand.Parameters.Add(para_text2);
            }
            if (text3.Length != 0)
            {
                para_text3 = new SqlParameter("@text3", text3);
                myDa.SelectCommand.Parameters.Add(para_text3);
            }

            if (text4.Length != 0)
            {
                para_text4 = new SqlParameter("@text4", text4);
                myDa.SelectCommand.Parameters.Add(para_text4);
            }

            if (text5.Length != 0)
            {
                para_text5 = new SqlParameter("@text5", text5);
                myDa.SelectCommand.Parameters.Add(para_text5);
            }


            //创建DataSet用来储存数据
            DataSet DS = new DataSet();
            //将数据填充到DataSet中
            myDa.Fill(DS, "performtable");
            dataGridView1.DataSource = DS.Tables["performtable"];
            //关闭连接
            cnn.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

     

        private void button2_Click_1(object sender, EventArgs e)
        {
            String text1 = textBox1.Text.Trim();
            String text2 = textBox2.Text.Trim();
            String text3 = comboBox1.Text.Trim();
            String text4 = textBox4.Text.Trim();
            String text5 = textBox5.Text.Trim();

            if (text1.Length == 0 || text2.Length == 0)
            {
                MessageBox.Show("修改失败！在修改房主信息前请填写房主的身份证号和姓名！");
                return;
            }

            SqlConnection cnn = new SqlConnection(constr);
            cnn.Open();
            string sqlstr = "update 房主 set ";
            if (text3.Length != 0)
            {

                sqlstr += " 性别= '" + text3 + "' ";
            }
            if (text4.Length != 0)
            {
                if (text3.Length != 0)
                    sqlstr += " ,";
                    sqlstr += " 生日= '" + text4 + "' ";
            }
            if (text5.Length != 0)
            {

                sqlstr += " ,电话号码= '" + text5 + "' ";
            }
            sqlstr += "where 房主身份证号= '" + text1 + "' and 姓名= '" + text2 + "'    ";

            SqlCommand sqlcmd = new SqlCommand(sqlstr, cnn);

            sqlcmd.ExecuteNonQuery();
            MessageBox.Show("修改用户信息成功！");
            string find = "select * from 房主 ";
            SqlDataAdapter myDa = new SqlDataAdapter(find, cnn);
            DataSet DS = new DataSet();
            //将数据填充到DataSet中
            myDa.Fill(DS, "performtable");
            // 在dataGridView中显示
            dataGridView1.DataSource = DS.Tables["performtable"];
            //关闭连接
            cnn.Close();



        }
    }
}
